package com.yk.mr.controller.job;

import com.yk.mr.service.job.api.IJobService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by Limit on 2017/8/23.
 */
@Controller
@RequestMapping("/job")
public class JobController {
    @Resource(name = "jobService")
    private IJobService jobService;


    @RequestMapping("createJobManual")
    @ResponseBody
    public boolean createJobManual(
            @RequestParam(name = "jobName") String jobName
            ,@RequestParam(name = "jobType",defaultValue = "") String jobType
            ,@RequestParam(name = "parameters",defaultValue = "")String parameters
            ,@RequestParam(name = "jarPath",defaultValue = "")String jarPath
            ,@RequestParam(name = "jobInput",defaultValue = "")String jobInput
            ,@RequestParam(name = "jobOutput",defaultValue = "")String jobOutput
    ) throws Exception {
        return this.jobService.saveJob(jobName,jobType,parameters,jarPath,jobInput,jobOutput);
    }

    @RequestMapping("createJob")
    @ResponseBody
    public boolean createJob(
            @RequestParam(name = "jobName") String jobName
            ,@RequestParam(name = "jobType",defaultValue = "") String jobType
            ,@RequestParam(name = "parameters",defaultValue = "")String parameters
    ) throws Exception {
        return this.jobService.saveJob(jobName,jobType,parameters);
    }

    @RequestMapping("deleteJob")
    @ResponseBody
    public Object deleteJob( @RequestParam(name = "jobId") String jobId){
        return this.jobService.deleteJob(jobId);
    }

    @RequestMapping("updateJob")
    @ResponseBody
    public Object updateJob( @RequestParam(name = "jobId") String jobId
            ,@RequestParam(name = "input") String input
            ,@RequestParam(name = "output") String output){
        return this.jobService.updateJob(jobId,input,output);
    }

    @RequestMapping("listJob")
    @ResponseBody
    public Object listJob(@RequestParam(name = "start",defaultValue = "0") int start,@RequestParam(name = "limit",defaultValue = "10") int limit) throws JSONException {
        JSONObject result=new JSONObject();
        result.put("sum",this.jobService.querySum());
        result.put("start",start);
        result.put("limit",limit);
        result.put("result",this.jobService.getJobModel(start,limit));
        return result;
    }

    @RequestMapping("listAll")
    @ResponseBody
    public Object listAll() throws JSONException {
        JSONObject result=new JSONObject();
        result.put("result",this.jobService.getJobModel());
        return this.jobService.getJobModel();
    }

    @RequestMapping("getJobIO")
    @ResponseBody
    public Object getJobIO(@RequestParam(name = "jobId") String jobId) throws JSONException {
        return this.jobService.getSelectFile(new JSONArray(this.jobService.getJobModel(jobId).getJobInput()));
    }

    @RequestMapping("getJob")
    @ResponseBody
    public Object getJob(@RequestParam(name = "jobId") String jobId) {
        return this.jobService.getJobModel(jobId);
    }

    @RequestMapping("getAppResource")
    @ResponseBody
    public Object getAppResource(@RequestParam(name = "jobType")String jobType){
        return null;
    }

}
